package cn.edu.sdjzu.xg.eagle.Controller;

import cn.edu.sdjzu.xg.eagle.Service.TeacherService;
import cn.edu.sdjzu.xg.eagle.entity.Condition;
import cn.edu.sdjzu.xg.eagle.entity.Pagination;
import cn.edu.sdjzu.xg.eagle.entity.Teacher;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import cn.edu.sdjzu.xg.eagle.util.ControllerHelper;
import cn.edu.sdjzu.xg.eagle.util.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;


@WebServlet("/teacherRead")
public class TeacherReadCtl extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request , HttpServletResponse response)
            throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        JSONObject message = new JSONObject();

        try {
            // 解析请求体，将请求体转为 JSON 对象
            String jsonStr = JSONUtil.getJSON(request);
            JSONObject requestJSon = JSONObject.parseObject(jsonStr);

            // 处理请求为空的情况
            if (requestJSon == null) {
                message.put("message","未收到请求信息");
                response.getWriter().write(message.toJSONString());
                return;
            }

            //获取条件列表
            List<Condition> conditions = ControllerHelper.getConditions(requestJSon);


            Collection<Teacher> teachers = TeacherService.findTeachersWithCondition( conditions);

            // 构建符合要求的响应格式
            JSONObject result = new JSONObject();
            result.put("totalNum",teachers.size());
            result.put("records",teachers);

            // 打印响应
            response.getWriter().write(result.toJSONString());

        } catch (BaseException e) {
            message.put("message", e.getMessage());
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            message.put("message", "网络异常");
            response.getWriter().println(message);
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        JSONObject message = new JSONObject();
        // 将 request 转化为 JSON 子串
        String req_json = JSONUtil.getJSON(request);
        // 将 req_json 子串转化为 JSON对象
        JSONObject requestJSon = JSONObject.parseObject(req_json);

        // 处理请求为空的情况
        if (requestJSon == null) {
            message.put("message","未收到请求信息");
            response.getWriter().write(message.toJSONString());
            return;
        }

        try {
            //读取分页信息
            Pagination pagination =  ControllerHelper.getPagination(requestJSon);
            //条件列表
            List<Condition> conditions = ControllerHelper.getConditions(requestJSon);

            Collection<Teacher> teachers = TeacherService.findTeachersWithPagination(pagination,conditions);

            message.put("totalNum",pagination.getTotalNum());
            message.put("records",teachers);

            //响应到前端
            response.getWriter().write(message.toJSONString());

        } catch (BaseException e) {
            message.put("message", e.getMessage());
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            message.put("message","网络异常");
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }
}
